package com.jdbc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository("AccountDao")
public class AccountDaoImpl implements AccountDao {
    //定义JdbcTemplate对象
    @Autowired
    private JdbcTemplate jdbcTemplate;
//
//    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
//        this.jdbcTemplate = jdbcTemplate;
//    }
    //添加记录

    @Override
    public int addAccount(Account account) {
        String sql = "insert spring.account (username,balance) values(?,?)"; //设置参数对象
        Object args[] = new Object[]{account.getUsername(), account.getBalance()};
        int row = jdbcTemplate.update(sql, args);
        return row;
    }//按id修改记录

    @Override
    public int updateAccountById(Account account) {
        String sql = "update spring.account set username=?,balance=? where id=?"; //设置参数对象
        Object args[] = new Object[]{account.getUsername(), account.getBalance(), account.getId()};
        int row = jdbcTemplate.update(sql, args);
        return row;
    }

    //按id修改存款金额
    @Override
    public int updateBalanceById(int id, double balance) {
        String sql = "update spring.account set balance=? where id=?";
        Object args[] = new Object[]{balance, id};
        int row = jdbcTemplate.update(sql, args);
        return row;
    }

    //删除记录
    @Override
    public int deleteAccountById(int id) {
        String sql = "delete from spring.account where id=?";
        int row = jdbcTemplate.update(sql, id);
        return row;
    }

}
